<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div class="home">
        <img src="../../assets/images/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 左边栏 -->
      <el-aside :width="collapse ? '64px':'200px'">
        <div class="nav-bar" @click="collapse=!collapse">
          |||
        </div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF"
        :unique-opened="true" :collapse="collapse" :collapse-transition="false" :router="true">
          <el-submenu :index="item.id + ''" v-for="(item, i) in listHome" :key="item.id" :style="collapse ? 'width:64px':'width:200px'">
            <template slot="title">
              <i :class="['iconfont', iconList[i]]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/'+subitem.path" class="el-icon-menu" v-for="subitem in item.children" :key="subitem.id">
              {{subitem.authName}}
            </el-menu-item>
            </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      listHome: '',
      iconList: ['icon-users', 'icon-tijikongjian', 'icon-shangpin', 'icon-danju', 'icon-baobiao'],
      collapse: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    logout () {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getList () {
      const {data: res} = await this.$http.get('menus')
      // console.log(res.data)
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.listHome = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
  .el-header {
    background: #373d41;
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0;
    padding-right: 20px;
    .home {
      display: flex;
      color: #fff;
      font-size: 22px;
      align-items: center;
      user-select: none;
      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    user-select: none;
  }
  .el-main {
    background-color: #eaedf1;
  }
}
.nav-bar {
  color: #fff;
  font-size: 12px;
  text-align: center;
  line-height: 25px;
  background: #4a5064;
  user-select: none;
  cursor: pointer;
  letter-spacing: 0.1em;
}
</style>
